package com.nce.web;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.nce.entity.Words;
import com.nce.service.WordsService;

/**
 * 用户管理Action.
 * 
 * 使用Struts2 convention-plugin annotation定义Action参数.
 * 演示带分页的管理界面.
 * 
 * @author GT
 */
//定义URL映射对应/account/user.action
@Namespace("/admin")
//定义名为reload的result重定向到user.action, 其他result则按照convention默认.
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "words.action", type = "redirect"), @Result(name = "home", location = "/", type = "redirect") })
public class WordsAction extends CrudActionSupport<Words> {

	private static final long serialVersionUID = 8683878162525847072L;

	@Autowired
	private WordsService wordsService;

	//-- 页面属性 --//
	private Long id;
	private Words entity = new Words();
	private Page<Words> page = new Page<Words>(20);//每页5条记录
	
	private String path;
	private String sdateStr;
	private String edateStr;
	
	
	

	public String getSdateStr() {
		return sdateStr;
	}

	public void setSdateStr(String sdateStr) {
		this.sdateStr = sdateStr;
	}

	public String getEdateStr() {
		return edateStr;
	}

	public void setEdateStr(String edateStr) {
		this.edateStr = edateStr;
	}

	//-- ModelDriven 与 Preparable函数 --//
	public void setId(Long id) {
		this.id = id;
	}

	public Words getModel() {
		return entity;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = wordsService.getWords(id);
		} else {
			entity = new Words();
		}
	}
	


	//-- CRUD Action 函数 --//
	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		//设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("words" );
			page.setOrder(Page.ASC);
		}
		page = wordsService.findWords(page, filters);
		
		Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
        calendar.add(Calendar.DATE, -1);    //得到前一天
        sdateStr= new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        calendar.add(Calendar.DATE, 2);    //得到后一天
        edateStr= new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());

		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() {
		if(entity.getId()==null){
			entity.setCtime(new Date());
		}
		wordsService.saveWords(entity);
		addActionMessage("保存敏感词成功");
		return RELOAD;
		
	}
		
	
	public void prepareReg() throws Exception {
		prepareModel();
	}

	@Override
	public String delete() throws Exception {
		wordsService.deleteWords(id);
		addActionMessage("删除敏感词成功");
		return RELOAD;
	}

	


	

	public WordsService getWordsService() {
		return wordsService;
	}

	public void setWordsService(WordsService wordsService) {
		this.wordsService = wordsService;
	}

	public void setPage(Page<Words> page) {
		this.page = page;
	}
	
	

	public Page<Words> getPage() {
		return page;
	}

	public Words getEntity() {
		return entity;
	}

	public void setEntity(Words entity) {
		this.entity = entity;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}
	
}
